//
//  YMHealthFileViewController.m
//  YMMedical
//
//  Created by y s on 2023/7/20.
//

#import "YMHealthFileViewController.h"
#import "YMHealthFileChooseTypeView.h"
#import "YMHealthFileViewDateView.h"
#import "YMHealthFileRecordListCell.h"
#import "YMHomeTipInforAlert.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "YMHomeService.h"


@interface YMHealthFileViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) YMHealthFileChooseTypeView *vChoose;
@property (nonatomic, strong) UITableView *tvMain;
@property (nonatomic, strong) YMHealthFileViewDateView *tabHeader;

@property (nonatomic, strong) NSMutableArray *arrRecord;
@property (nonatomic, copy) NSString *chooseDate;


@end

@implementation YMHealthFileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.vChoose refreshData];
}

- (void)setUI{
    self.fd_interactivePopDisabled = YES;
    [self showNavTitle:KLanguage(@"health_docu_txt")];
    [self.view addSubview:self.vChoose];
    [self.view addSubview:self.tvMain];
    
    [self.vChoose mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.height.mas_equalTo([SFitUtil fit:50]);
    }];
    [self.tvMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.vChoose.mas_bottom).offset([SFitUtil fit:15]);
        make.left.right.bottom.mas_equalTo(0);
    }];
     
}


- (void)deleteClickAction:(NSIndexPath *)indexPath{
    //删除操作
    NSDictionary *dict = [SDataUtil dictOf:self.arrRecord[indexPath.section]];
    NSString *detectionId = [SDataUtil stringOf:dict[@"detectionId"] valid:@""];
    
    WEAK_SELF;
    [YMHomeService requestHomeDeleteHealthReportWithId:detectionId success:^{
        [weakSelf.arrRecord removeObjectAtIndex:indexPath.section];  //删除数组里的数据
        [weakSelf.tabHeader updateData:weakSelf.chooseDate arrRecord:weakSelf.arrRecord];
        [weakSelf.tvMain deleteRow:0 inSection:indexPath.section withRowAnimation:UITableViewRowAnimationLeft];
        [weakSelf.tvMain reloadData];
    } failure:^(NSString * _Nonnull message) {
        [SHUD showInfo:message];
    }];
}

#pragma mark -- UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.arrRecord.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YMHealthFileRecordListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YMHealthFileRecordListCell" forIndexPath:indexPath];
    if (indexPath.section < self.arrRecord.count){
        cell.dict = [SDataUtil dictOf:self.arrRecord[indexPath.section]];
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [YMHealthFileRecordListCell getCellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return [SFitUtil fit:10];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section < self.arrRecord.count){
        NSDictionary *dict = [SDataUtil dictOf:self.arrRecord[indexPath.section]];
        NSString *detectionId = [SDataUtil stringOf:dict[@"detectionId"] valid:@""];
        [YMBridging goToHealthTestReportVC:NO detectionId:detectionId target:nil];
    }
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    WEAK_SELF;
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:KLanguage(@"remove_txt") handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
        YMHomeTipInforAlert *alert = [[YMHomeTipInforAlert alloc] init];
        alert.iconName = @"alert_tips_infor_delete_icon";
        alert.strContent = KLanguage(@"confirm_remove_record_txt");
        alert.sureBlock = ^{
            [weakSelf deleteClickAction:indexPath];
        };
        [alert show];
    }];
    return @[deleteAction];
}

/**自定义设置iOS11系统下的左滑删除按钮大小*/
//开始编辑左滑删除
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    if (@available(iOS 13.0, *)) {
        for (UIView *subView in self.tvMain.subviews) {
            if([subView isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")]) {
                for (UIView *subViewss in subView.subviews) {
                    if([subViewss isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
                        subViewss.backgroundColor = [SUIUtil colorOf:0xFF3141];
                        for (UIView *subview3 in subViewss.subviews) {
                            if([subview3 isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
                                UIButton *btn = (UIButton *)subview3;
                                btn.backgroundColor = [SUIUtil colorOf:0xFF3141];
                                [btn setTitle:KLanguage(@"remove_txt") forState:UIControlStateNormal];
                                [btn setTitleColor:STheme.D9 forState:UIControlStateNormal];
                                btn.titleLabel.font = [SFitUtil fitBoldFont:15];
                            }
                        }
                    }
                }
            }
        }
    }else if (@available(iOS 11.0, *)) {
        for (UIView *subViews in self.tvMain.subviews) {
            if([subViews isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
                subViews.backgroundColor = [SUIUtil colorOf:0xFF3141];
                for (UIView *subViewss in subViews.subviews) {
                    if([subViewss isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
                        UIButton *btn = (UIButton *)subViewss;
                        btn.backgroundColor = [SUIUtil colorOf:0xFF3141];
                        [btn setTitle:KLanguage(@"remove_txt") forState:UIControlStateNormal];
                        [btn setTitleColor:STheme.D9 forState:UIControlStateNormal];
                        btn.titleLabel.font = [SFitUtil fitBoldFont:15];
                    }
                }
            }
        }
    }
}
//判断是否显示左滑删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (self.arrRecord.count > 0){
//        return YES;
//    }
    return NO;
}

#pragma mark -- setter and getter
- (UITableView *)tvMain{
    if (!_tvMain) {
        _tvMain = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tvMain.backgroundColor = [UIColor clearColor];
        _tvMain.delegate   = self;
        _tvMain.dataSource = self;
        _tvMain.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tvMain.showsVerticalScrollIndicator   = NO;
        _tvMain.showsHorizontalScrollIndicator = NO;
        _tvMain.estimatedRowHeight = 0;
        _tvMain.estimatedSectionHeaderHeight = 0;
        _tvMain.estimatedSectionFooterHeight = 0;
        _tvMain.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _tvMain.tableHeaderView = self.tabHeader;
        [_tvMain registerClass:[YMHealthFileRecordListCell class] forCellReuseIdentifier:@"YMHealthFileRecordListCell"];
    }
    return _tvMain;
}
- (YMHealthFileChooseTypeView *)vChoose{
    if (_vChoose == nil){
        _vChoose = [[YMHealthFileChooseTypeView alloc] init];
        _vChoose.testeeId = self.testeeId;
        _vChoose.idStr = self.idStr;
        _vChoose.maxIdStr = self.maxIdStr;
        WEAK_SELF;
        _vChoose.changeItemlock = ^(NSString * _Nonnull minorTermId) {
            weakSelf.tabHeader.minorTermId = minorTermId;
            [weakSelf.tabHeader loadData];
        };
        _vChoose.changeUserlock = ^(NSString * _Nonnull userId) {
            weakSelf.tabHeader.detectionUserId = userId;
            [weakSelf.tabHeader loadData];
        };
    }
    return _vChoose;
}
- (YMHealthFileViewDateView *)tabHeader{
    if (_tabHeader == nil){
        _tabHeader = [[YMHealthFileViewDateView alloc] init];
        WEAK_SELF;
        _tabHeader.chooseDateBlock = ^(NSString * _Nonnull dateStr, NSArray * _Nonnull arrRecord) {
            weakSelf.chooseDate = dateStr;
            [weakSelf.arrRecord removeAllObjects];
            [weakSelf.arrRecord addObjectsFromArray:arrRecord];
            [weakSelf.tvMain reloadData];
        };
    }
    return _tabHeader;
}
- (NSMutableArray *)arrRecord{
    if (_arrRecord == nil){
        _arrRecord = [[NSMutableArray alloc] init];
    }
    return _arrRecord;
}

@end
